package com.swyc.osschat.Test.shijianlun;

import com.swyc.osschat.Common.Util.JsonUtils;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @description 时间轮算法-抽象定时任务
 * @Author: z156940l@outlook.com
 * @className AbstractNettyDelayQueueListener
 * @Date: 2023/3/14 15:28
 */
@Slf4j
@Data
public abstract class AbstractNettyDelayQueueListener<T> implements TimerTask {

    @Override
    public void run(Timeout timeout) throws Exception {
        try {
            long start = System.currentTimeMillis();
            log.info("开始执行业务逻辑，data：{}", JsonUtils.toJson(taskVO));
            execute(taskVO);
            log.info("执行业务逻辑结束，总耗时：{}ms", System.currentTimeMillis() - start);
        }catch (Exception e){
            log.error("执行延时任务逻辑系统异常！errMsg：{}", e.getMessage());
        }
    }

    /**
     * 具体的业务执行逻辑
     * @param taskVO
     */
    protected abstract void execute(TaskVO<T> taskVO);

    /**
     * 任务VO对象
     */
    public TaskVO<T> taskVO;

    public void buildData(T t){
        TaskVO task = new TaskVO<>();
        task.setData(t);
        this.taskVO = task;
    }
}
